<!--定义登录组件-->

<template>
  <div class="login-container">
    <el-form v-if="debug" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <h3 class="title" style="margin-right:145px;">登录智慧体测实验室</h3>
      </div>

      <!--输入用户名及密码-->
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="请输入用户名" style="width: 240px;" />
      </el-form-item>

      <el-form-item prop="password" style="margin-top:10px;">
        <el-input v-model="loginForm.password" prefix-icon="el-icon-s-cooperation" type="password" placeholder="请输入密码" style="width: 240px;" />
      </el-form-item>

      <!--登录按钮-->
      <el-button
        :loading="loading"
        type="primary"
        style="width: 60%"
        @click.native.prevent="login"
      >登录</el-button>
    </el-form>

  </div>
</template>

<script>
import Settings from '@/settings'
import AuthUserApi from '@api/auth/user'
import { setToken } from '@/utils/auth'
import md5 from 'js-md5'

export default {
  name: 'Login',
  data() {
    return {
      // 登录表单对象
      loginForm: {
        username: undefined, // 用户名
        password: undefined// 密码
      },
      // 表单验证规则
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      // 加载动画
      loading: false,
      hasToken: false,
      debug: false
    }
  },
  created() {
    this.debug = Settings.debug
  },
  mounted() {
    // this.checkToken()
  /*  // 如果用户名为空，则焦点聚焦到用户名输入框
    if(this.loginForm.username === ''){
      this.$refs.username.focus()
      // 如果密码为空，则焦点聚焦到密码输入框
    }else if(this.loginForm.password === ''){
      this.$refs.password.focus()
    }*/
  },
  destroyed() {
  },
  methods: {
    // 用户名密码登录
    login() {
      // loginForm表单验证
      this.$refs.loginForm.validate(valid => {
        // 验证成功
        if (valid) {
          this.loading = true  //显示加载动画

          const data = {}
          data.username = this.loginForm.username
          data.password = md5(this.loginForm.password)
          // AuthUserApi.checkToken(data)
          //   .then(data =>{
          //
          //   }).finally(()=>{
          //       this.loading = false
          // })
          // 派发到store下user.js的user/checkToken  action
          // dispatch 触发action中的函数，并传递参数this.loginForm
          this.$store.dispatch('user/login', data)
            .then(() => { // 登陆成功
              // 路由到首页，指定query参数
              this.$router.replace({ path: '/', query: this.otherQuery })
              this.loading = false
              this.hasToken = true
            }).catch(() => { // 异常
              this.loading = false
              this.hasToken = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 选择用户登录
    onSelectUser(val) {
      // 调用@api/auth/user中的createDebugToken()方法
      AuthUserApi.createDebugToken(val).then(response => {
        response = response.data
        if (response.code === 0) {
          const { data } = response
          // 设置token
          setToken(data.token)
          this.$router.replace({ path: '/', query: this.otherQuery })
        } else {
          this.$message({ message: 'Token创建失败', type: 'error' })
        }
      }).catch(() => {
        this.$message({ message: 'Token创建失败', type: 'error' })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #1a3342;
  $light_gray: #308a8d;

  .login-container {
    min-height: 100%;
    width: 100%;
    //登录页背景图
    background-image: url(~@/assets/login.png);
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-size: cover;
    overflow: hidden;

    .login-form {
      position: relative;
      width: 400px;
      max-width: 100%;
      top: 35%;
      left: 65%;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 28px;
        color: $light_gray;
        margin: 0px auto 20px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }
</style>
